<?php

namespace App\Admin\Resources;
use App\Admin\Models\SystemModel;

/**
 * @mixin \App\Admin\Models\Config
 */
class ConfigResource extends JsonResource
{
    public const FOR_INDEX = 'index';
    public const FOR_SHOW = 'show';
    public const FOR_EDIT = 'edit';
    public const FOR_CONFIG = 'config';

    public function toArray($request)
    {
        $typeMap = array_merge(SystemModel::$inputTypeMap, SystemModel::$searchTypeMap);
      
        $data = [
            'category' => $this->mergeConfigCategory(),
            'type_text' => $typeMap[$this->type],
        ];
        return array_merge(parent::toArray($request),$data);
    }

    protected function mergeConfigCategory()
    {
        return [
            $this->mergeFor(static::FOR_INDEX, function () {
                return $this->category()->withTrashed(isRestore())->first();
            }),
            $this->mergeFor(static::FOR_EDIT, function () {
                return $this->category()->withTrashed(isRestore())->first();
            }),
            $this->mergeFor(static::FOR_SHOW, function () {
                return $this->category()->withTrashed(isRestore())->first();
            }),
            $this->mergeFor(static::FOR_CONFIG, function () {
                return $this->category()->withTrashed(isRestore())->first();
            }),
        ];
    }
}
